﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace NightskyMimic.Domain
{
    public class Controller
    {
        public Player Player1 { get; set; }

        public Player Player2 { get; set; }

        public Controller(Player player1, Player player2) 
        {
            Player1 = player1;
            Player2 = player2;
        }

        public Player Start()
        {
            Player1.Begin();
            Player2.Begin();
            int turn = 1;

            while(!GameOver()) 
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine(" ");
                Console.WriteLine("begin " + Player1.ToString() + " turn "+ turn + " life: " + Player1.Zone.Life);
                PlayTurn(Player1, Player2);
                Console.WriteLine("End of turn");
                Console.ReadKey();
                if(GameOver())
                    break;
                Console.ForegroundColor = ConsoleColor.Red;
               Console.WriteLine(" ");
               Console.WriteLine("begin " + Player2.ToString() + " turn " + turn + " life: " + Player2.Zone.Life);
                PlayTurn(Player2, Player1);
                Console.WriteLine("End of turn");
                Console.ReadKey();
                turn++;
            }

            if (Player1.IsAlive)
            {
                Console.WriteLine(Player1.ToString() + " wins");
                return Player1;
            }
            else
            {
                Console.WriteLine(Player2.ToString() + " wins");
                return Player2;
            }
        }

        private void PlayTurn(Player player1, Player player2)
        {
            player1.BegginingPhase();
            List<Attack> attack = player1.Strategy.GenerateAttackActions(player1, player2);
            attack.ForEach(x => x.Target.Tapped = true);
            log(attack.Select(x => x as Action).ToList());

            List<Block> block = player2.Strategy.GenerateBlockActions(player2, player1, attack);
            log(block.Select(x => x as Action).ToList());

            List<List<Creature>> deadCreatures = Battle.Combat(attack, block);
            removeDeadCreatures(deadCreatures, player1, player2);

            List<Attack> notBLockedAttacks = attack.Where(x => !x.Resolved).ToList();
            int totalDamage = Battle.CombatDamage(notBLockedAttacks);
            player2.Zone.Life -= totalDamage;

            player1.MainPhase();
        }

        private void log(List<Action> actions)
        {
            foreach (Action a in actions) 
                Console.WriteLine(a.ToString());
        }

        private void removeDeadCreatures(List<List<Creature>> deadCreatures, Player player1, Player player2)
        {
            List<Creature> dead1 = deadCreatures[1];

            dead1.ForEach(x => { player1.Zone.PutInGraveyard(x); Console.WriteLine(x.ToString() + " is dead"); });

            List<Creature> dead2 = deadCreatures[0];
            dead2.ForEach(x => { player2.Zone.PutInGraveyard(x); Console.WriteLine(x.ToString() + " is dead"); });
        }

        private bool GameOver()
        {
            return !Player1.IsAlive || !Player2.IsAlive;
        }
    }
}